package com.Liux.dao;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

//操作数据库的公共类
public class BaseDao {

    private static String driver;
    private static String url;
    private static String username;
    private static String password;
    //静态代码块 ，类加载的时候初始化
    static {
        Properties properties = new Properties();
        //通过类加载器读取对应的资源
        InputStream is = BaseDao.class.getClassLoader().getResourceAsStream("db.properties");

        try {
            properties.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }

        driver = properties.getProperty("driver");
        url = properties.getProperty("url");
        username = properties.getProperty("username");
        password = properties.getProperty("password");


    }
    //获取数据库的链接
    public  static Connection getConnection(){
        Connection connection = null;
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return  connection;
    }
    //编辑查询公共类
    public static ResultSet excute(Connection connection,PreparedStatement preparedStatement,  ResultSet resultSet ,String sql,Object[] params) throws SQLException {
       preparedStatement = connection.prepareStatement(sql);

        for (int i = 0; i < params.length; i++) {
            //setObject ，占位符从1开始，但是我们的数组是从0开始
            preparedStatement.setObject(i+1,params[i]);
        }
        resultSet = preparedStatement.executeQuery();
        return resultSet;
    }
    //编写增删改查公共方法
    public static int excute(Connection connection, String sql, Object[] params,  PreparedStatement preparedStatement) throws SQLException {
        preparedStatement = connection.prepareStatement(sql);

        for (int i = 0; i < params.length; i++) {
            //setObject ，占位符从1开始，但是我们的数组是从0开始
            preparedStatement.setObject(i+1,params[i]);
        }
        int updateRow = preparedStatement.executeUpdate();
        return updateRow;
    }
    //释放资源
    public static boolean closeResource(Connection connection,PreparedStatement preparedStatement,ResultSet resultSet){
        boolean flag = true;
        if(resultSet!=null){
            try {
                resultSet.close();
                resultSet = null;
            } catch (SQLException throwables) {
                flag=false;
                throwables.printStackTrace();
            }
        }
        if(preparedStatement!=null){
            try {
                resultSet.close();
                //GC回收
                preparedStatement = null;

            } catch (SQLException throwables) {
                flag = false;
                throwables.printStackTrace();
            }
        }
        if(connection!=null){
            try {
                connection.close();
               connection=null;
            } catch (SQLException throwables) {
                flag = false;
                throwables.printStackTrace();
            }
        }
        return  flag;
    }


}
